package com.songtech.config;

import java.nio.charset.Charset;
import java.util.List;

import com.songtech.shiro.annotation.UserInfoArgumentResolver;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.songtech.base.filter.AccessFilter;
import com.songtech.base.interceptor.BaseInterceptor;
import com.songtech.base.listener.SessionListener;

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{

	
	/**
	 * 设置页面访问路径
	 * @return
	 */
	@Bean  
    public InternalResourceViewResolver viewResolver(){  
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();  
        viewResolver.setPrefix("/WEB-INF/asserts/page/");  
        viewResolver.setSuffix(".jsp");  
        viewResolver.setViewClass(JstlView.class);  
        return  viewResolver;  
    }  
	
	/**
	 * spring上传文件设置
	 * @return
	 */
	@Bean  
    public MultipartResolver multipartResolver(){  
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();  
        multipartResolver.setMaxUploadSize(1000000);
        multipartResolver.setDefaultEncoding("UTF-8");
        return multipartResolver;  
    }  
	
	/**
	 * 
	 * 设置默认请求跳转
	 */
//	@Override
//	public void addViewControllers(ViewControllerRegistry registry) {  
//        registry.addViewController("/").setViewName("forward:/login.jsp");
//        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
//        super.addViewControllers(registry);
//    }  
	
	/**
	 * 静态资源处理
	 */
	@Override  
    public void addResourceHandlers(ResourceHandlerRegistry registry) {  
        //addResourceLocations指的是文件放置的目录，addResourceHandler指的是对外暴露的访问路径  
        registry.addResourceHandler("/asserts/resource/**").addResourceLocations("/WEB-INF/asserts/resource/");
        super.addResourceHandlers(registry);
    }
	
	
	/**
	 * 设置中文乱码
	 * @return
	 */
	@Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter converter = 
        		new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(responseBodyConverter());
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }
    
    @Bean  
    public ServletListenerRegistrationBean<SessionListener> addListenerRegistration(){  
        ServletListenerRegistrationBean<SessionListener> registration = new ServletListenerRegistrationBean<SessionListener>();  
        registration.setListener(new SessionListener());
        registration.setName("sessionListener");
        return registration;  
    }
   
    
    @Bean
	public FilterRegistrationBean addFilterRegistration() {
	   FilterRegistrationBean registration = new FilterRegistrationBean();
	   registration.setFilter(new AccessFilter());
	   registration.addUrlPatterns("/*");
	   registration.setName("accessFilter");
	   registration.setOrder(1);
	   return registration;
	 }
    
    @Bean
    public BaseInterceptor baseInterceptor() {
      return new BaseInterceptor();
    }
    
    @Override  
    public void addInterceptors(InterceptorRegistry registry) {  
        //注册自定义拦截器，添加拦截路径和排除拦截路径  
        registry.addInterceptor(baseInterceptor()).addPathPatterns("/**").excludePathPatterns("/login","/logout","/druid/*");
        super.addInterceptors(registry);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new UserInfoArgumentResolver());
        super.addArgumentResolvers(argumentResolvers);
    }
}
